﻿namespace YAF.Editors
{
    using System;
    using System.Data;
    using System.Web.Compilation;

    using Modules;

    public class YafEditorModuleManager : YafModuleManager<BaseForumEditor>
    {
        private YafEditorModuleManager()
            : base("YAF.Editors", "YAF.Editors.IBaseEditorModule")
        {
            if (this.ModuleClassType == null)
            {
                this.AddModules(BuildManager.CodeAssemblies);
            }
        }

        public BaseForumEditor GetEditorInstance(int moduleId)
        {
            this.Load();

            foreach (BaseForumEditor editor in this.Modules)
            {
                if (editor.ModuleId == moduleId)
                {
                    return editor;
                }
            }

            return null;
        }

        public DataTable GetEditorsTable()
        {
            this.Load();

            using (var dt = new DataTable("Editors"))
            {
                dt.Columns.Add("Value", Type.GetType("System.Int32"));
                dt.Columns.Add("Name", Type.GetType("System.String"));

                foreach (BaseForumEditor editor in this.Modules)
                {
                    if (editor.Active)
                    {
                        dt.Rows.Add(
                            new object[]
                            {
                                editor.ModuleId, editor.Description
                            });
                    }
                }

                return dt;
            }
        }
    }
}
